package com.ming.yygh.cmn.controller;

import com.ming.yygh.cmn.service.DictService;
import com.ming.yygh.common.result.Result;
import com.ming.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author Light
 * @date 2021/11/28 22:02
 */
@Api(tags = "数据字典")
@RestController
@RequestMapping("admin/cmn/dict")
//@CrossOrigin
public class DictController {
    @Resource
    private DictService dictService;

    // 导出数据字典
    @GetMapping("/exportData")
    public void exportDict(HttpServletResponse response) {
        dictService.exportDictData(response);
    }

    // 导入数据字典
    @PostMapping("/importData")
    public Result<Boolean> importData(MultipartFile file) {
        dictService.importDictData(file);
        return Result.ok();
    }

    // 根据数据id查询子数据列表
    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping("/findChildData/{id}")
    public Result<List<Dict>> findChildData(@PathVariable(value = "id") String id) {
        List<Dict> dictList = dictService.findChildData(id);
        return Result.ok(dictList);
    }

    // 根据dictCode和value值查询
    @ApiOperation(value = "根据dictCode和value值查询")
    @GetMapping("/getName/{dictCode}/{value}")
    public Result<String> getName(@PathVariable(value = "dictCode") String dictCode,
                                  @PathVariable(value = "value") String value) {
        Dict dict = dictService.getDictName(dictCode, value);
        return Result.ok(dict.getName());
    }

    // 根据value值查询
    @ApiOperation(value = "根据value值查询")
    @GetMapping("/getName}/{value}")
    public Result<String> getName(@PathVariable(value = "value") String value) {
        Dict dict = dictService.getDictName("", value);
        return Result.ok(dict.getName());
    }

    // 根据dictCode获取下级节点
    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping("/findByDictCode/{dictCode}")
    public Result<List<Dict>> findByDictCode(@PathVariable(value = "dictCode") String dictCode) {
        List<Dict> dictList = dictService.findByDictCode(dictCode);
        return Result.ok(dictList);
    }

}
